package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TestModule;
import com.ruoyi.system.service.ITestModuleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 测试模块Controller
 * 
 * @author ruoyi
 * @date 2023-09-25
 */
@Controller
@RequestMapping("/system/module")
public class TestModuleController extends BaseController
{
    private String prefix = "system/module";

    @Autowired
    private ITestModuleService testModuleService;

    @RequiresPermissions("system:module:view")
    @GetMapping()
    public String module()
    {
        return prefix + "/module";
    }

    /**
     * 查询测试模块树列表
     */
    @RequiresPermissions("system:module:list")
    @PostMapping("/list")
    @ResponseBody
    public List<TestModule> list(TestModule testModule)
    {
        List<TestModule> list = testModuleService.selectTestModuleList(testModule);
        return list;
    }

    /**
     * 导出测试模块列表
     */
    @RequiresPermissions("system:module:export")
    @Log(title = "测试模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TestModule testModule)
    {
        List<TestModule> list = testModuleService.selectTestModuleList(testModule);
        ExcelUtil<TestModule> util = new ExcelUtil<TestModule>(TestModule.class);
        return util.exportExcel(list, "测试模块数据");
    }

    /**
     * 新增测试模块
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("testModule", testModuleService.selectTestModuleById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存测试模块
     */
    @RequiresPermissions("system:module:add")
    @Log(title = "测试模块", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TestModule testModule)
    {
        return toAjax(testModuleService.insertTestModule(testModule));
    }

    /**
     * 修改测试模块
     */
    @RequiresPermissions("system:module:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TestModule testModule = testModuleService.selectTestModuleById(id);
        mmap.put("testModule", testModule);
        return prefix + "/edit";
    }

    /**
     * 修改保存测试模块
     */
    @RequiresPermissions("system:module:edit")
    @Log(title = "测试模块", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TestModule testModule)
    {
        return toAjax(testModuleService.updateTestModule(testModule));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:module:remove")
    @Log(title = "测试模块", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(testModuleService.deleteTestModuleById(id));
    }

    /**
     * 选择测试模块树
     */
    @GetMapping(value = { "/selectModuleTree/{id}", "/selectModuleTree/" })
    public String selectModuleTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("testModule", testModuleService.selectTestModuleById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载测试模块树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = testModuleService.selectTestModuleTree();
        return ztrees;
    }
}
